/**
 * Copyright 2012 Gerardo Cortes Oquendo.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package co.pixellabs.gear.hash;

/**
 * @author gerardo.cortes.o@gmail.com
 * 
 */
public final class HashGenerator
{
    public static Computer compute(final int seed)
    {
	return new Computer(seed);
    }

    public final static class Computer
    {
	private static final int PRIME;
	private int result;

	static
	{
	    PRIME = 31;
	}

	public Computer(int seed)
	{
	    this.result = seed;
	}

	public final int generate()
	{
	    return result;
	}

	public Computer add(final boolean b)
	{
	    compute((b ? 1 : 0));
	    return this;
	}

	public Computer add(final byte b)
	{
	    compute((int) b);
	    return this;
	}

	public Computer add(final char c)
	{
	    compute((int) c);
	    return this;
	}

	public Computer add(final short s)
	{
	    compute((int) s);
	    return this;
	}

	public Computer add(final int i)
	{
	    compute(i);
	    return this;
	}

	public Computer add(final long l)
	{
	    compute((int) (l ^ (l >>> 32)));
	    return this;
	}

	public Computer add(final float f)
	{
	    compute(Float.floatToIntBits(f));
	    return this;
	}

	public Computer add(final double d)
	{
	    return add(Double.doubleToLongBits(d));
	}

	public Computer add(final Object o)
	{
	    compute(o.hashCode());
	    return this;
	}

	private void compute(final int c)
	{
	    result = PRIME * result + c;
	}
    }
}
